package com.chazz.servlet;

import com.chazz.entity.User;
import com.chazz.service.UserService;
import com.chazz.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/retrieve.do")
public class RetrieveServlet extends HttpServlet {
    private static UserService userService=new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String password=req.getParameter("password");
        String email=req.getParameter("email");
        String yzm=req.getParameter("yzm");
        HttpSession session=req.getSession();
        String stringBuilder= (String) session.getAttribute("passwordstringBuilder");
        PrintWriter out=resp.getWriter();
        if(stringBuilder.equals(yzm)){
            try {

                 User user=userService.findUserByEmail(email);
                user.setPassword(password);
                boolean flag=userService.updateUserPassword(user);
                if(flag){
                    out.write("true");
                }else{
                    out.write("updatefalse");
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        }else{
            out.write("false");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
